package com.voicechat.firstmet.activity;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.LoginService;
import com.voicechat.firstmet.utils.MD5Utils;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.TimerUtil;
import com.voicechat.firstmet.utils.Utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: BindingPhoneActivity
 * @Description: 绑定手机号和修改密码页面
 * @Author: ljp
 * @CreateDate: 2018/9/20 9:22
 * @Version: 1.0
 */
public class BindingPhoneActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.et_phone_number)
    EditText etPhoneNumber;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_password_sure)
    EditText etPasswordSure;
    @BindView(R.id.register_sure)
    TextView registerSure;
    @BindView(R.id.et_code)
    EditText etCode;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private TimerUtil mTimer;
    private String phoneNumber;
    private String TAG = "BindingPhoneActivity";

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_bind_phone;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        phoneNumber = SPUtils.getEditoString(SPUtils.SP_PHONE);
        initView();

    }

    private void initView() {
        mTimer = new TimerUtil(60, tvGetCode, this);
        etPhoneNumber.setText(phoneNumber);
        etPhoneNumber.setEnabled(false);

    }

    /**
     * @return
     * @method
     * @description 获取验证码
     * @date: 2018/11/6 10:16
     * @author: ljp
     */
    private void setCode() {
        LoginService.loginGetVCode(phoneNumber, LoginService.VALIDATE_CODE_TYPE.VALIDATE_CODE_CHANGE).subscribe(result -> {
            if (result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                mTimer.myStart(0);
                toast_MakeText(result.getMsg());
            } else if (result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            } else {
                toast_MakeText(result.getMsg());
            }
        }, throwable -> {
            toast_MakeText(getResources().getString(R.string.net_error));
        });
    }

    @OnClick({R.id.linear_back, R.id.tv_get_code, R.id.register_sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.tv_get_code:
                setCode();
                break;
            case R.id.register_sure:
                setAlterPass();
                break;
        }
    }

    /**
     * @return
     * @method
     * @description 修改密码
     * @date: 2018/12/12 9:49
     * @author: ljp
     */
    private void setAlterPass() {
        String passWord = etPassword.getText().toString().trim();
        String passWord2 = etPasswordSure.getText().toString().trim();
        String code = etCode.getText().toString().trim();
        Pattern pattern = Pattern.compile("[0-9]*");

        Pattern p = Pattern.compile("^[a-zA-Z].*[0-9]|.*[0-9].*[a-zA-Z]");
        Matcher m = p.matcher(passWord);
        if (!(passWord.length() >= 6 && passWord.length() <= 16)) {
            toast_MakeText("密码长度为6-16位字符!");
            return;
        }
        if (pattern.matcher(passWord.substring(0, 1)).matches()) {
            toast_MakeText("密码以字母开头,请重新输入!");
            return;
        }
        if (pattern.matcher(passWord).matches()) {
            toast_MakeText("密码不能为纯数字或纯字母,请重新输入!");
            return;
        }
        if (Utils.isChar(passWord)) {
            toast_MakeText("密码不能为纯字母,请重新输入!");
            return;
        }
        String confirmpassword = etPasswordSure.getText().toString().replace(" ", "");
        if (TextUtils.isEmpty(confirmpassword) || TextUtils.equals(confirmpassword, "")) {
            toast_MakeText("请再次输入密码!");
            return;
        }
        if (!TextUtils.equals(passWord, confirmpassword)) {
            toast_MakeText("两次密码不一致,请重新输入!");
            return;
        }
        if (TextUtils.isEmpty(code) || code.length() < 4){
            toast_MakeText("请输入正确的验证码");
            return;
        }
        String md5PassWord = MD5Utils.getMd5Value(passWord);
        LoginService.alterPassword(phoneNumber, md5PassWord, PhoneUtils.getDeviceInfo(context),code,SPUtils.getEditoString(SPUtils.SP_TOKEN)).subscribe(result -> {
           // Log.i(TAG, "setAlterPass: ----------"+result.toString());
                if (result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                    toast_MakeText("密码修改成功");
                    finish();
                } else if (result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                    Utils.setToken(context);
                } else {
                    toast_MakeText(result.getMsg());
                }
        }, throwable -> {
            toast_MakeText(getResources().getString(R.string.net_error));
        });

    }


}
